package acwing;

import java.io.*;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class ac851 {
    static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    static final int N = 100010, INF = 0x3f3f3f3f;
    static int n, m, idx;
    static int[] h = new int[N], ne = new int[N], e = new int[N], w = new int[N];
    static int[] d = new int[N];
    static boolean[] st = new boolean[N];

    static int cin() throws IOException {
        in.nextToken();
        return (int) in.nval;
    }

    static void add(int a, int b, int c) {
        e[idx] = b;
        ne[idx] = h[a];
        w[idx] = c;
        h[a] = idx ++ ;
    }

    static int spfa() {
        Queue<Integer> q = new LinkedList<>();
        q.offer(1);
        d[1] = 0;
        st[1] = true;
        while (!q.isEmpty()) {
            int t = q.poll();
            st[t] = false;
            for (int i = h[t]; i != -1; i = ne[i]) {
                int j = e[i];
                if (d[j] > d[t] + w[i]) {
                    d[j] = d[t] + w[i];
                    if (!st[j]) {
                        q.offer(j);
                        st[j] = true;
                    }
                }
            }
        }
        return d[n];
    }

    public static void main(String[] args) throws IOException {
        n = cin(); m = cin();
        Arrays.fill(h, -1);
        Arrays.fill(d, INF);
        while ( m -- > 0 ) {
            int a = cin(), b = cin(), c = cin();
            add(a, b, c);
        }
        int res = spfa();
        if (res == INF) out.println("impossible");
        else out.println(res);
        out.close();
    }
}